//
//  DUSelectYearMonthView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/26.
//

import Cocoa


@IBDesignable
class DUSelectYearMonthView: NSView, SimpleChangeHandler, DUMonthDayProtocol {
    
    private var buttons = RadioGroup<DUButton>()
    
    var page:PageTypeModel?
    let yearMax:Int = 2199
    let yearMin:Int = 1900
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    private func  initialize() {
        var y = 253
        for _ in (0...3) {
            var x = 10
            for _ in (0...2) {
                let d = DUButton(frame: NSRect(x: x, y: y, width: 112, height: 71))
                buttons.addSubview(view: d)
                addSubview(d)
                x += 122
            }
            y -= 81
        }
        buttons.change = self
    }
    
    func setMonth(year:Int, month:Int) -> PageTypeModel {
        var i = 1
        let c:Bool = CalendarUtil.getCurrentYear() == year
        buttons.clean(defaultSelect: false)
        for v:DUButton in buttons.findAll() {
            setButtonValue(button: v, type: .MONTH, value: "\(i)月", data: i,
                           yearStart: year, yearEnd: year, month: i, enable: false, selected: c && i == month)
            i+=1
        }
        return PageTypeModel(pageType: .MONTH, yearStart: year, yearEnd: year, month: 0)
    }
    
    private func setButtonValue(button: DUButton, type:PageSwitchEnum, value:String,data:Int,
                                yearStart:Int,yearEnd:Int,month:Int, enable:Bool, selected:Bool) {
        button.stringValue(value, data: data)
        button.enabled = false
        let p = PageTypeModel(pageType: type, yearStart: yearStart, yearEnd: yearEnd, month: month)
        button.target = p
        if selected {
            button.defaultSelect = true
            button.selected = true
            button.setStyle(button.mouseDownStyle)
        }
    }
    
    func setYear(year:Int) -> PageTypeModel {
        var p = PageTypeModel()
        p.pageType = .YEAR
        var ys = year/10 * 10
        p.yearStart = ys
        buttons.clean(defaultSelect: false)
        var i = 1
        let v1 = buttons.getSubview(index: 0)
        if ys == yearMin{
            setButtonValue(button: v1, type: .YEAR, value: "", data: -1,
                           yearStart: ys-1, yearEnd: ys - 1, month: 0, enable: true, selected: false)
        } else {
            setButtonValue(button: v1, type: .YEAR, value: "\(ys-1)年", data: -1,
                           yearStart: ys-1, yearEnd: ys - 1, month: 0, enable: false, selected: false)
        }
        
        while i < buttons.count() - 1 {
            let v2 = buttons.getSubview(index: i)
            setButtonValue(button: v2, type: .YEAR, value: "\(ys)年", data: ys,
                           yearStart: ys, yearEnd: ys, month: 0, enable: false, selected: ys == year)
            ys += 1
            i += 1
        }
        let v3 = buttons.getSubview(index: i)
        if ys == (yearMax + 1) {
            setButtonValue(button: v3, type: .YEAR, value: "", data: -1,
                       yearStart: ys, yearEnd: ys, month: 0, enable: true, selected: false)
        } else {
            setButtonValue(button: v3, type: .YEAR, value: "\(ys)年", data: -1,
                       yearStart: ys, yearEnd: ys, month: 0, enable: false, selected: false)
        }
        p.yearEnd = ys - 1
        return p
    }
    
    func setYearSlot(year:Int) -> PageTypeModel {
        var p = PageTypeModel()
        p.pageType = .MANY_YEAR
        var ys = year/100 * 100
        p.yearStart = ys
        buttons.clean(defaultSelect: false)
        var i = 1
        let v1 = buttons.getSubview(index: 0)
        if ys == yearMin {
            setButtonValue(button: v1, type: .MANY_YEAR, value: "", data: -1,
                       yearStart: 0, yearEnd: 0, month: 0, enable: true, selected: false)
        } else {
            let ys2 = (ys - 1)/10 * 10
            setButtonValue(button: v1, type: .MANY_YEAR, value: "\(ys2)- \(ys2+9)", data: -1,
                       yearStart: ys2, yearEnd: ys2+9, month: 0, enable: false, selected: false)
        }
        while i < buttons.count() - 1 {
            let v2 = buttons.getSubview(index: i)
            setButtonValue(button: v2, type: .MANY_YEAR, value: "\(ys)- \(ys+9)", data: ys*10000+ys+9,
                           yearStart: ys, yearEnd: ys+9, month: 0, enable: false, selected: (ys <= year && (ys + 9) >= year))
            ys += 10
            i += 1
        }
        let v3 = buttons.getSubview(index: i)
        if ys == (yearMax + 1) {
            setButtonValue(button: v3, type: .MANY_YEAR, value: "", data: -1,
                       yearStart: 0, yearEnd: 0, month: 0, enable: true, selected: false)
        } else {
            setButtonValue(button: v3, type: .MANY_YEAR, value: "\(ys)- \(ys+9)", data: -1,
                       yearStart: ys, yearEnd: ys+9, month: 0, enable: false, selected: false)
        }
        p.yearEnd = ys - 1
        return p
    }
    
    func setPage(pageType: PageSwitchEnum, year:Int, month:Int, completionHandler completion: ((_ type:PageTypeModel?) -> Void)) {
        if year < yearMin || year > yearMax  {
            //print(year)
            completion(_: nil)
            return
        }
        var p = PageTypeModel()
        switch pageType {
        case .MONTH:
            p = setMonth(year: year, month: month)
        case .YEAR:
            p = setYear(year: year)
        case .MANY_YEAR:
            p = setYearSlot(year: year)
        case .DAY:
            p = PageTypeModel()
        }
        page = p
        completion(p)
    }
    
    func update(date: DateEntity) {
        
    }
    
    func prevPage() {
        
    }
    
    func nextPage() {
        
    }
    
    func currentMonth() {
        
    }
    
    func setMonth(year: Int, month: Int) {
        
    }
    
    
    
    func onChange(_ params: NSObject?) {
        if params != nil {
            let pd = params as! DUButton
            GlobalControl.shared.page?.switchButtonHandler(page: pd.target!)
        }
    }
}
